********************************************************************************
*                             STRUCTURAL ANALYSIS                              *
*                          This version: 29 Oct 2021                           *
********************************************************************************


clear all
set more off

* 0. Working Directory and Paths

cd "${Do}/structural"

global path : pwd
global input "${Data}/structural/input"
global intermediate "${Data}/structural/intermediate"
global output_s "${Data}/structural/output"
global output "${Output}/structural"


* 1. Create Data

do "1.1_Create_data_for_matlab_stups.do"
do "1.2_Create_data_for_matlab_nonstups.do"
do "1.3_Create_data_for_matlab_w3-5.do"


* 2. Structural Analysis in Matlab

local mf : dir "." files "*.m", respectcase

foreach file in `mf' {
	local file = subinstr("`file'", ".m", "", 1)
	disp "Currently running `file'"
	!matlab -r -wait "try; cd '$path'; `file'; quit; catch E; rethrow(E); end;"
}


* 3. Outputs (Numbers for Text and Graphs)

*** Numbers in Subsection VI.B
do "3.1_Calibration.do"

*** Parameter descriptives (Figures B.10, B.11, B.3 panel c))
do "3.2_Parameters.do"

*** Figure VII
do "3.3_Test_w3.do"
do "3.4_Test_w4.do"
do "3.5_Test_w5.do"

*** Numbers in Subsubsection VI.D and Figure VIII
do "3.6_Misallocation.do"


* 4. Counterfactuals

local cf : dir "./counterfactual codes/" files "*.m", respectcase

foreach file in `cf' {
	local file = subinstr("`file'", ".m", "", 1)
	disp "Currently running `file'"
	!matlab -r -wait "try; cd '$path'; addpath('counterfactual codes/'); `file'; quit; catch E; rethrow(E); end;"
}


* 5. Outputs of Counterfactuals

*** Numbers in Subsection VI.D
do "5.1_Counterfactuals_numbers.do"

*** Figure B.13 
do "5.2_Counterfactuals_figures.do" 
